/* 

	What does popNavigationItem return?

*/

Plugins.load("UIKit");
include("jiggy.magic.js");
include("jiggy.objects.js");

var PageController = {};

PageController.init = function (backgroundColor, navigationItemFont) {
	backgroundColor = backgroundColor || Color.white;
	navigationItemFont = navigationItemFont || UINavigationItem.defaultFont;

	this.windowRect = Rectangle(UIHardware.fullScreenApplicationContentRect);
	this.baseRect = Rectangle(Point(0,0), this.windowRect.getSize());
	this.navBarRect = Rectangle(0, 0, this.baseRect.getWidth(), UINavigationBar.defaultHeight);

	this.viewRect = Rectangle(this.baseRect.getX(), this.navBarRect.getHeight(), this.baseRect.getWidth(), this.baseRect.getHeight() - this.navBarRect.getHeight());
	this.pageRect = Rectangle(0, 0, this.baseRect.getWidth(), this.baseRect.getHeight() - this.navBarRect.getHeight());

	this.window = new UIWindow(this.windowRect);
	this.window.setHidden( false );
	this.window.orderFront();
	this.window.makeKey();
	this.window.backgroundColor = backgroundColor;

	this.baseView = new UIView(this.baseRect);
	this.baseView.backgroundColor = backgroundColor;
	this.window.setContentView(this.baseView);

	this.pages = {};
	
	// Setup the navbar
	this.navBar = new UINavigationBar(this.navBarRect);
	this.navBar.setBarStyle(UINavigationBar.barStyles.normal);
	this.navBar.showButtonsWithLeftTitle(null, null, false);
	this.navBar.onButtonClicked = this.navBar_onButtonClicked;
		
	this.transitioner = new UITransitionView(this.viewRect);
	
	this.baseView.addSubview(this.navBar);	
	this.baseView.addSubview(this.transitioner);
};

PageController.navBar_onButtonClicked = function(bar, button) {
	if (button === 0) {
		PageController.turnPage();
		log("Left Button Pressed");
	} else {
		PageController.turnPageBack();
		log("Right Button Pressed");
	}
};



PageController.addPage = function (page) {
	if (!(page instanceof Page)) {
		throw "Only instances of Page can be added to the PageController's page collection";
	}
	
	// Check for duplicate name
	if (typeof(PageController.pages[page.name]) !== "undefined") {
		throw "page name already in use.";
	}
	
	this.pages[page.name] = page;
	this.transitioner.addSubview(page.view);
};


/*
	Shows a page without animation and with the option to clear the stack.
*/
PageController.showPage = function (page, clearStack) {
	page = (page instanceof Page) ? page : null;
	clearStack = typeof(clearStack) === "undefined" ? true : !!clearStack;
	
	// Handle page names or pages
	if (page === null) {
		throw("showPage can only show instances of Page");
	}
	
	if (typeof(this.pages[page.name]) === "undefined") {
		throw("showPage can only show pages that have been added to the PageController");
	}

	// Cleanup the previously displayed page
	if (this.navBar.topItem) {
		//this.navBar.topItem.page.view.frame = this.rightOffScreenRect;
	}


	// Update the navigation bar
	this.navBar.disableAnimation();
	if (clearStack) {
		
		while (this.navBar.navigationItems.length) {
			this.navBar.popNavigationItem();
		}
		if (page.backPage) {
			var backPageNavigationItem = new UINavigationItem(page.backPage.title);
			backPageNavigationItem.setFont(this.navigationItemFont);
			backPageNavigationItem.page = page.backPage;
			this.navBar.pushNavigationItem(backPageNavigationItem);
		}
	}

	var nextPageTitle = (page.nextPage && page.nextPage.title) || null;
	var lastPageTitle = (this.navBar.topItem && this.navBar.topItem.page.title) || null;
	
	var pageNavigationItem = new UINavigationItem(page.title);
	pageNavigationItem.setFont(this.navigationItemFont);
	pageNavigationItem.page = page;

	this.navBar.pushNavigationItem(pageNavigationItem);
	this.navBar.showButtonsWithLeftTitle(lastPageTitle, nextPageTitle, !!lastPageTitle);
	this.navBar.enableAnimation();
	
	// Display the new page
	this.transitioner.transition(UITransitionView.styles.immediate, this.navBar.topItem.page.view);
	
	log(this.navBar.topItem.page.view.frame);

};

PageController.turnPageAnimationTime = 0.25;
PageController.turnPage = function (page) {
	log("Turning Page");
	page = (page instanceof Page) ? page : ((this.navBar.topItem && this.navBar.topItem.page.nextPage) || null);
	
	// Handle page names or pages
	if (page === null) {
		throw("showPage can only show instances of Page");
	}
	if (typeof(this.pages[page.name]) === "undefined") {
		throw("showPage can only show pages that have been added to the PageController");
	}

	// Update the navigation bar and page stack
	var pageNavigationItem = new UINavigationItem(page.title);
	pageNavigationItem.setFont(this.navigationItemFont);
	pageNavigationItem.page = page;


	var nextPageTitle = (page.nextPage && page.nextPage.title) || null;
	var lastPageTitle = (this.navBar.topItem && this.navBar.topItem.page.title) || null;

	var currentPage = (this.navBar.topItem && this.navBar.topItem.page) || null;

	this.navBar.pushNavigationItem(pageNavigationItem);
	this.navBar.showButtonsWithLeftTitle(lastPageTitle, nextPageTitle, !!lastPageTitle);
	
	if (currentPage !== null) {
		this.transitioner.transition(UITransitionView.styles.shiftLeft, page.view);
	} else {
		this.transitioner.transitionFrom(UITransitionView.styles.shiftLeft, currentPage.view, page.view);
	}

/*	Animator.stopAnimation();
	if (currentPage !== null) {
		var currentPageFrameAnimation = new UIFrameAnimation(currentPage.view, 0, PageController.onScreenRect, PageController.leftOffScreenRect);
		Animator.addAnimation(currentPageFrameAnimation, PageController.turnPageAnimationTime, true);
	}
	var nextPageFrameAnimation = new UIFrameAnimation(page.view, 0, PageController.rightOffScreenRect, PageController.onScreenRect);
	Animator.addAnimation(nextPageFrameAnimation, PageController.turnPageAnimationTime, true);
*/
};

PageController.turnPageBack = function () {
	log("Turning Page Back");
	if (!!!this.navBar.topItem) {
		throw("There are no pages to turn to or from.");
	}

	var currentPage = (this.navBar.topItem && this.navBar.topItem.page) || null;
	var page = (this.navBar.navigationItems[this.navBar.navigationItems.length - 2] &&
		this.navBar.navigationItems[this.navBar.navigationItems.length - 2].page) || null;
	
	if (page === null) {
		throw("There is no page to go back to.");
	}
	
	var nextPageTitle = (page.nextPage && page.nextPage.title) || null;
	var lastPageTitle = (this.navBar.navigationItems[this.navBar.navigationItems.length - 3] &&
		this.navBar.navigationItems[this.navBar.navigationItems.length - 3].page.title) || null;


	this.navBar.showButtonsWithLeftTitle(lastPageTitle, nextPageTitle, !!lastPageTitle);
	this.navBar.popNavigationItem();

	if (currentPage !== null) {
		this.transitioner.transition(UITransitionView.styles.shiftRight, page.view);
	} else {
		this.transitioner.transitionFrom(UITransitionView.styles.shiftRight, currentPage.view, page.view);
	}

	
/*
	Animator.stopAnimation();
	var currentPageFrameAnimation = new UIFrameAnimation(currentPage.view, 0, PageController.onScreenRect, PageController.rightOffScreenRect);
	Animator.addAnimation(currentPageFrameAnimation, PageController.turnPageAnimationTime, true);

	var nextPageFrameAnimation = new UIFrameAnimation(page.view, 0, PageController.leftOffScreenRect, PageController.onScreenRect);
	Animator.addAnimation(nextPageFrameAnimation, PageController.turnPageAnimationTime, true);
*/
};

// Creates a new page and view.	When the Page is added to the PageController its view is added
// to the baseView.
// backPage is only used in showPage when the stack is cleared.
function Page(name, title, backgroundColor, nextPage, backPage) {
	this.name = name || null;

	if (this.name === null) {
		throw("Page name is required");
	}


	this.title = title || null;
	backgroundColor = backgroundColor || Color.white;
	this.nextPage = nextPage || null;
	this.backPage = backPage || null;
	
	this.view = new UIView(PageController.pageRect);
	this.view.backgroundColor = backgroundColor;
}
// Shortcut for adding subviews to the UIView
Page.prototype.addSubview = function (view) {
	this.view.addSubview(view);
};
